﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QldtSdh.GUI.DaoTao
{
    using BLL;
    using DAL;
    using DAL.Models;
    using GUI.Utils;
    using GUI.Common;
    using Utils;

    public partial class FormResourcesViewer : QldtSdh.GUI.Common.BaseDialog
    {
        private UcLopHocPhan_ThoiKhoaBieu _Parent;
        private static FormResourcesViewer _Instance = null;
        public static FormResourcesViewer Instance
        {
            get
            {
                if (_Instance == null || _Instance.IsDisposed)
                {
                    _Instance = new FormResourcesViewer() { StartPosition = FormStartPosition.Manual };
                    if (LastLocation.X != -1 || LastLocation.Y != -1)
                        _Instance.Location = LastLocation;
                    else
                        _Instance.Location = new Point(40, Screen.PrimaryScreen.WorkingArea.Bottom - _Instance.Height - 40);
                }
                return _Instance;
            }
        }
        public static bool IsShowing
        {
            get { return _Instance != null && !_Instance.IsDisposed && _Instance.Visible; }
        }

        public static void MinimizeInstance()
        {
            if (_Instance != null && _Instance.IsDisposed == false)
                _Instance.WindowState = FormWindowState.Minimized;
        }

        public static void RestoreInstance()
        {
            if (_Instance != null && _Instance.IsDisposed == false)
                _Instance.WindowState = FormWindowState.Normal;
        }

        private static Point LastLocation = new Point(-1, -1);

        private bool _DoNotLoadThoiKhoaBieu = false;

        private static Color NORMAL_BACKCOLOR = Color.White;
        private static Color SELECTED_BACKCOLOR = Color.LightSteelBlue;
        private static Color BUSY_NORMAL_BACKCOLOR = Color.Snow;
        private static Color BUSY_SELECTED_BACKCOLOR = Color.MistyRose;

        private DateTime _ThuHai;
        private DateTime _CN { get { return _ThuHai.AddDays(6).Add(new TimeSpan(23, 59, 59)); } }

        private LopHocPhan _LopHocPhan;

        public string SelectedMaGiangVien
        {
            get
            {
                LopHocPhan_PhanCongGiangDay x = null;
                if (bsGiangVien.Current != null)
                    x = bsGiangVien.Current as LopHocPhan_PhanCongGiangDay;
                if (x == null)
                    return null;
                else
                    return x.MaGiangVien;
            }
        }

        public string SelectedMaPhongHoc
        {
            get
            {
                PhongHoc phongHoc = null;
                if (bsPhongHoc.Current != null)
                    phongHoc = bsPhongHoc.Current as PhongHoc;
                if (phongHoc == null)
                    return null;
                else
                    return phongHoc.MaPhongHoc;
            }
        }

        private LopHocPhan_PhanCongGiangDay _GiangVien
        {
            get
            {
                LopHocPhan_PhanCongGiangDay pc = null;
                if (bsGiangVien.Current != null)
                    pc = bsGiangVien.Current as LopHocPhan_PhanCongGiangDay;
                return pc;
            }
        }

        private PhongHoc _PhongHoc
        {
            get
            {
                PhongHoc p = null;
                if (bsPhongHoc.Current != null)
                    p = bsPhongHoc.Current as PhongHoc;
                return p;
            }
        }

        private Label[][] _Values;

        private Label _CurrentCell = null;
        public void SetCurrentCell(Label label, bool doNotSetBackForParent = false)
        {
            if (_Values == null)
                return;
            _CurrentCell = label;
            if (label == null)
                return;
            foreach (var items in _Values)
                foreach (var item in items)
                {
                    if (label.Name == item.Name)
                    {
                        if (item.Name.Contains("8"))
                            item.BackColor = BUSY_SELECTED_BACKCOLOR;
                        else
                            item.BackColor = SELECTED_BACKCOLOR;
                    }
                    else
                    {
                        if (item.Name.Contains("8"))
                            item.BackColor = BUSY_NORMAL_BACKCOLOR;
                        else
                            item.BackColor = NORMAL_BACKCOLOR;
                    }
                    //item.BackColor = label.Name == item.Name ? SELECTED_BACKCOLOR : NORMAL_BACKCOLOR;
                }

            if (doNotSetBackForParent)
                return;
            if (_Parent != null && !_Parent.IsDisposed)
                _Parent.SetCurrentCell(label, doNotSetBackForFormResourceViewer: true);
        }

        public void SetThuHai(DateTime date)
        {
            #region _ThuHai = ...(date);
            date = date.Date;
            _ThuHai = date.Subtract(new TimeSpan(BizThoiKhoaBieu.GetIndexFromDOW(date.DayOfWeek), 0, 0, 0));
            #endregion

            this.Text = string.Format("Tài nguyên TKB {0} ÷ {1}", _ThuHai.ToString("dd/MM"), _CN.ToString("dd/MM"));

            //lblCH2.Text = string.Format("Thứ 2\r\n{0}", _ThuHai.AddDays(0).ToString("dd/MM"));
            //lblCH3.Text = string.Format("Thứ 3\r\n{0}", _ThuHai.AddDays(1).ToString("dd/MM"));
            //lblCH4.Text = string.Format("Thứ 4\r\n{0}", _ThuHai.AddDays(2).ToString("dd/MM"));
            //lblCH5.Text = string.Format("Thứ 5\r\n{0}", _ThuHai.AddDays(3).ToString("dd/MM"));
            //lblCH6.Text = string.Format("Thứ 6\r\n{0}", _ThuHai.AddDays(4).ToString("dd/MM"));
            //lblCH7.Text = string.Format("Thứ 7\r\n{0}", _ThuHai.AddDays(5).ToString("dd/MM"));
            //lblCH8.Text = string.Format("CN\r\n{0}", _ThuHai.AddDays(6).ToString("dd/MM"));

            if (_DoNotLoadThoiKhoaBieu)
                return;
            NapDanhSachThoiKhoaBieu();
        }

        public void SetLopHocPhan(LopHocPhan lopHocPhan, List<LopHocPhan_PhanCongGiangDay> _ListLopHocPhan_PhanCongGiangDay,
            List<PhongHoc> _ListPhongHoc)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                this._LopHocPhan = lopHocPhan;

                bsPhongHoc.DataSource = _ListPhongHoc;
                cbxPhonghoc.DisplayMember = "MaPhongHoc";
                cbxPhonghoc.DataSource = bsPhongHoc;

                cbxGiangvien.DisplayMember = "_GiangVienDescTKB";
                bsGiangVien.DataSource = _ListLopHocPhan_PhanCongGiangDay;
                cbxGiangvien.DataSource = bsGiangVien;

                if (_DoNotLoadThoiKhoaBieu)
                    return;
                NapDanhSachThoiKhoaBieu();
            }
        }

        public void NapDanhSachThoiKhoaBieu()
        {
            for (int i = 0; i <= 6; i++)
                for (int j = 0; j <= 2; j++)
                {
                    _Values[j][i].Tag = _ThuHai.AddDays(i);
                    _Values[j][i].Text = "";
                }

            var maGiangVien = _GiangVien == null ? "" : _GiangVien.MaGiangVien;
            var maGiangVienDaoTaoDaiHoc = (maGiangVien == "" || !Program.KiemTraThoiKhoaBieuDaiHoc) ? "" : BizGiangVien.GetByID2(maGiangVien).MaGiangVienDaoTaoDaiHoc;
            var maPhongHoc = _PhongHoc == null ? "" : _PhongHoc.MaPhongHoc;
            var tuNgay = _ThuHai;
            var denNgay = _CN;
            string maLopHocPhan = _LopHocPhan == null ? null : _LopHocPhan.MaLopHocPhanGUID;

            try
            {
                string[][] arr = BizThoiKhoaBieu.GetLichBanGiangVienPhongHoc(maLopHocPhan, maGiangVien, maGiangVienDaoTaoDaiHoc, maPhongHoc, tuNgay, denNgay, Program.KiemTraThoiKhoaBieuDaiHoc);
                //string[][] arr = BizThoiKhoaBieu.GetLichBanGiangVienPhongHoc(maGiangVien, maPhongHoc, tuNgay, denNgay);
                for (int i = 0; i < 3; i++)
                    for (int j = 0; j < 7; j++)
                        _Values[i][j].Text = arr[i][j];
            }
            catch (Exception exc)
            {
                DialogUtils.ShowErrorWithDefaultTitle("Xuất hiện lỗi trong quá trình lấy thông tin tài nguyên thời khoá biểu.\nChi tiết lỗi: " + exc.Message);
            }
        }

        public static void ShowFor(UcLopHocPhan_ThoiKhoaBieu parent, LopHocPhan lopHocPhan,
            List<LopHocPhan_PhanCongGiangDay> _ListLopHocPhan_PhanCongGiangDay,
            List<PhongHoc> _ListPhongHoc,
            DateTime thuHai)
        {
            var instance = Instance;
            instance._DoNotLoadThoiKhoaBieu = true;
            instance._Parent = parent;
            instance.SetThuHai(thuHai);
            instance.SetLopHocPhan(lopHocPhan, _ListLopHocPhan_PhanCongGiangDay, _ListPhongHoc);

            instance._DoNotLoadThoiKhoaBieu = false;
            instance.NapDanhSachThoiKhoaBieu();
            instance.SetCurrentCell(parent.GetCurrentCell(), doNotSetBackForParent: true);
            instance.Show();
        }

        public static void CloseIt()
        {
            if (_Instance != null && !_Instance.IsDisposed)
            {
                try { _Instance.Close(); }
                catch { }
            }
            _Instance = null;

        }

        private FormResourcesViewer()
        {
            InitializeComponent();

            _Values = new Label[][] {
                new Label[]{ lbl2S, lbl3S, lbl4S, lbl5S, lbl6S, lbl7S, lbl8S },
                new Label[]{ lbl2C, lbl3C, lbl4C, lbl5C, lbl6C, lbl7C, lbl8C },
                new Label[]{ lbl2T, lbl3T, lbl4T, lbl5T, lbl6T, lbl7T, lbl8T }};
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 7; j++)
                    _Values[i][j].MouseClick += Cell_MouseClick;
        }

        void Cell_MouseClick(object sender, MouseEventArgs e)
        {
            this.SetCurrentCell(sender as Label, false);
        }

        private void FormResourcesViewer_FormClosing(object sender, FormClosingEventArgs e)
        {
            LastLocation = this.Location;
        }

        private void bsPhongHoc_CurrentChanged(object sender, EventArgs e)
        {
            if (_DoNotLoadThoiKhoaBieu)
                return;
            NapDanhSachThoiKhoaBieu();

            lblPhongHocKeTiep.Enabled = bsPhongHoc.Position < bsPhongHoc.Count - 1;
            lblPhongHocTruoc.Enabled = bsPhongHoc.Position > 0;
        }

        private void bsGiangVien_CurrentChanged(object sender, EventArgs e)
        {
            if (_DoNotLoadThoiKhoaBieu)
                return;
            NapDanhSachThoiKhoaBieu();
        }

        private void lblPhongHocTruoc_Click(object sender, EventArgs e)
        {
            if (sender == lblPhongHocKeTiep)
            {
                if (bsPhongHoc.Position < bsPhongHoc.Count - 1)
                    bsPhongHoc.Position++;
            }
            else
            {
                if (bsPhongHoc.Position > 0)
                    bsPhongHoc.Position--;
            }
        }
    }
}
